home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / js / calWcapRequest.js < prev    next >
Encoding:
JavaScript  |  2007-05-23  |  14.5 KB  |  431 lines

  1. /* -*- Mode: javascript; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public
  6.  * License Version 1.1 (the "License"); you may not use this file
  7.  * except in compliance with the License. You may obtain a copy of
  8.  * the License at http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS
  11.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12.  * implied. See the License for the specific language governing
  13.  * rights and limitations under the License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is Sun Microsystems, Inc.
  18.  * Portions created by Sun Microsystems are Copyright (C) 2006 Sun
  19.  * Microsystems, Inc. All Rights Reserved.
  20.  *
  21.  * Original Author: Daniel Boelzle (daniel.boelzle@sun.com)
  22.  *
  23.  * Contributor(s):
  24.  *
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the NPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the NPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /**
  41.    Requests, either the queued calWcapRequest or an async network request.
  42.    
  43.    A request object is used to track an async action.
  44.    While the action is running, isPending is true.
  45.    Functions issuing an async action usually take a response function along
  46.    with their parameters, typically named respFunc.
  47.    That function is called *after* the action has ended (i.e. isPending of the
  48.    issued action/request is false when called, status remains stable).
  49.    The response function gets the ended request as first parameter to check
  50.    whether the request has been successful and get its data.
  51.    The request function itself may return either
  52.    - a further calIWcapRequest request object, i.e. an async continuation
  53.    - some data (incl null/undefined) which is the result of the async function,
  54.      indicating that there is no further continuation
  55. */
  56.  
  57. var g_requestId = 0;
  58. function generateRequestId() {
  59.     ++g_requestId;
  60.     return g_requestId;
  61. }
  62.  
  63. function calWcapRequest(respFunc, logContext) {
  64.     this.wrappedJSObject = this;
  65.     this.m_logContext = logContext;
  66.     this.m_id = generateRequestId();
  67.     this.m_isPending = true;
  68.     this.m_status = NS_OK;
  69.     this.m_respFunc = respFunc;
  70.     this.m_attachedRequests = [];
  71. }
  72. calWcapRequest.prototype = {
  73.     m_logContext: null,
  74.     m_parentRequest: null,
  75.     m_id: 0,
  76.     m_isPending: true,
  77.     m_status: NS_OK,
  78.     m_respFunc: null,
  79.     m_attachedRequests: null,
  80.     m_locked: false,
  81.     
  82.     get parentRequest() { return this.m_parentRequest; },
  83.     set parentRequest(req) {
  84.         if (this.parentRequest)
  85.             logError("already has parent!", this);
  86.         this.detachFromParent(); // detach without error
  87.         return (this.m_parentRequest = req);
  88.     },
  89.     
  90.     /** The following locking is necessary when scheduling multiple async
  91.         requests; one cannot be sure that e.g. the first completes quickly
  92.         and responds the whole parent request when detaching.
  93.     */
  94.     lockPending: function calWcapRequest_lockPending() {
  95.         this.m_locked = true;
  96.     },
  97.     unlockPending: function calWcapRequest_unlockPending() {
  98.         if (this.m_locked) {
  99.             this.m_locked = false;
  100.             // assures that respFunc is executed:
  101.             if (this.m_attachedRequests.length == 0) {
  102.                 this.execRespFunc();
  103.             }
  104.         }
  105.     },
  106.     
  107.     toString: function calWcapRequest_toString() {
  108.         var ret = ("calWcapRequest id=" + this.id +
  109.                    ", parent-id=" +
  110.                    (this.parentRequest ? this.parentRequest.id : "<none>") +
  111.                    " (" + this.m_logContext + ")");
  112.         if (LOG_LEVEL > 2 && this.m_attachedRequests.length > 0) {
  113.             ret += "\nattached requests:";
  114.             for each ( var req in this.m_attachedRequests ) {
  115.                 ret += ("\n#" + req.id + "\t" + req);
  116.             }
  117.         }
  118.         return ret;
  119.     },
  120.     
  121.     attachSubRequest: function calWcapRequest_attachSubRequest(req)
  122.     {
  123.         if (req) {
  124.             if (!this.m_attachedRequests.some(
  125.                     function(req_) { return req.id == req_.id; } )) {
  126.                 if (req.isPending) {
  127.                     this.m_attachedRequests.push(req);
  128.                     req.parentRequest = this;
  129.                     log("attachSubRequest()", this);
  130.                 }
  131.                 else if (!this.m_locked && this.m_attachedRequests.length == 0) {
  132.                     this.execRespFunc(req.status);
  133.                 }
  134.             }
  135.             else {
  136.                 logError("request already attached: " + req.id, this);
  137.             }
  138.         }
  139.     },
  140.     
  141.     detachSubRequest: function calWcapRequest_detachSubRequest(req, err)
  142.     {
  143.         this.m_attachedRequests = this.m_attachedRequests.filter(
  144.             function(req_) { return req.id != req_.id; } );
  145.         if (err) {
  146.             // first failing sub request stops whole request:
  147.             this.execRespFunc(err);
  148.         }
  149.         // assures that respFunc is executed after every sub request has been completed:
  150.         else if (!this.m_locked && this.m_attachedRequests.length == 0) {
  151.             this.execRespFunc();
  152.         }
  153.     },
  154.     
  155.     cancelAllSubRequests: function calWcapRequest_cancelAllSubRequests(status) {
  156.         var attachedRequests = this.m_attachedRequests;
  157.         this.m_attachedRequests = [];
  158.         attachedRequests.forEach( function(req) { req.cancel(status); } );
  159.     },
  160.     
  161.     detachFromParent: function calWcapRequest_detachFromParent(err) {
  162.         var parentRequest = this.m_parentRequest;
  163.         if (parentRequest) {
  164.             this.m_parentRequest = null;
  165.             parentRequest.detachSubRequest(this, err);
  166.         }
  167.     },
  168.     
  169.     execRespFunc: function calWcapRequest_execRespFunc(err, data)
  170.     {
  171.         if (this.isPending) {
  172.             this.m_isPending = false;
  173.             if (err)
  174.                 this.m_status = err;
  175.             this.cancelAllSubRequests();
  176.             var respFunc = this.m_respFunc;
  177.             if (respFunc) {
  178.                 this.m_respFunc = null; // call once only
  179.                 if (LOG_LEVEL > 2) {
  180.                     log("response exec: " + errorToString(err), this);
  181.                 }
  182.                 try {
  183.                     respFunc(this, err, data);
  184.                 }
  185.                 catch (exc) {
  186.                     this.m_status = exc;
  187.                     logError(exc, this);
  188.                 }
  189.             }
  190.             this.detachFromParent(this.m_status);
  191.         }
  192.     },
  193.     
  194.     // calIWcapRequest:
  195.     get id() {
  196.         return this.m_id;
  197.     },
  198.     get isPending() {
  199.         return this.m_isPending;
  200.     },
  201.     get succeeded() {
  202.         return (!this.isPending && Components.isSuccessCode( getResultCode(this.status) ));
  203.     },
  204.     get status() {
  205.         return (this.m_status === null ? NS_OK : this.m_status);
  206.     },
  207.     
  208.     cancel: function calWcapRequest_cancel(status)
  209.     {
  210.         if (this.isPending) {
  211.             this.m_isPending = false;
  212.             log("cancel.", this);
  213.             this.m_respFunc = null;
  214.             if (!status)
  215.                 status = Components.results.NS_ERROR_FAILURE;
  216.             this.m_status = status;
  217.             this.cancelAllSubRequests();
  218.             this.detachFromParent(); // detach without error
  219.         }
  220.     }
  221. };
  222.  
  223. function calWcapNetworkRequest(channel, respFunc, bLogging) {
  224. //     this.superClass(respFunc);
  225.     this.wrappedJSObject = this;
  226.     this.m_id = generateRequestId();
  227.     this.m_channel = channel;
  228.     this.m_respFunc = respFunc;
  229.     this.m_bLogging = (bLogging === undefined ? true : bLogging);
  230. }
  231. // subClass(calWcapNetworkRequest, calWcapRequest);
  232.  
  233. calWcapNetworkRequest.prototype = {
  234.     m_id: 0,
  235.     m_channel: null,
  236.     m_respFunc: null,
  237.     m_bLogging: false,
  238.     
  239.     m_isPending: true,
  240.     get isPending() { return this.m_isPending; },
  241.     
  242.     toString: function calWcapNetworkRequest_toString() {
  243.         var ret = ("calWcapNetworkRequest id=" + this.id +
  244.                    ", parent-id=" +
  245.                    (this.parentRequest ? this.parentRequest.id : "<none>"));
  246.         if (this.m_bLogging)
  247.             ret += (" (" + this.m_channel.URI.spec + ")");
  248.         return ret;
  249.     },
  250.     
  251.     m_parentRequest: null,
  252.     get parentRequest() { return this.m_parentRequest; },
  253.     set parentRequest(req) {
  254.         if (this.parentRequest)
  255.             logError("already has parent!", this);
  256.         this.detachFromParent(); // detach without error
  257.         return (this.m_parentRequest = req);
  258.     },
  259.     
  260.     get id() {
  261.         return this.m_id;
  262.     },
  263.     
  264.     detachFromParent: function calWcapNetworkRequest_detachFromParent(err) {
  265.         var parentRequest = this.m_parentRequest;
  266.         if (parentRequest) {
  267.             this.m_parentRequest = null;
  268.             parentRequest.detachSubRequest(this, err);
  269.         }
  270.     },
  271.     
  272.     cancel: function calWcapNetworkRequest_cancel(status) {
  273.         if (this.isPending) {
  274.             this.m_isPending = false;
  275.             log("cancel.", this);
  276.             this.m_respFunc = null;
  277.             this.detachFromParent(); // detach without error
  278.             // xxx todo: check whether this works on redirected channels!
  279.             if (this.m_channel.isPending()) {
  280.                 log("cancelling netwerk request...", this);
  281.                 this.m_channel.cancel(NS_BINDING_FAILED);
  282.             }
  283.         }
  284.     },
  285.     
  286.     execRespFunc: function calWcapNetworkRequest_execRespFunc(err, str)
  287.     {
  288.         if (this.isPending) {
  289.             this.m_isPending = false;
  290.             var respFunc = this.m_respFunc;
  291.             if (respFunc) {
  292.                 this.m_respFunc = null; // call once only
  293.                 if (LOG_LEVEL > 2 && this.m_bLogging) {
  294.                     log("response exec: " + errorToString(err), this);
  295.                 }
  296.                 try {
  297.                     respFunc(err, str);
  298.                     err = null; // may have been handled
  299.                 }
  300.                 catch (exc) {
  301.                     logError(exc, this);
  302.                     err = exc;
  303.                 }
  304.             }
  305.             this.detachFromParent(err);
  306.         }
  307.     },
  308.     
  309.     // nsIUnicharStreamLoaderObserver:
  310.     onDetermineCharset: function calWcapNetworkRequest_onDetermineCharset(
  311.         loader, context, firstSegment, length)
  312.     {
  313.         var channel = null;
  314.         if (loader)
  315.             channel = loader.channel;
  316.         var charset = null;
  317.         if (channel)
  318.             charset = channel.contentCharset;
  319.         if (!charset || charset.length == 0)
  320.             charset = "UTF-8";
  321.         return charset;
  322.     },
  323.     
  324.     onStreamComplete: function calWcapNetworkRequest_onStreamComplete(
  325.         loader, context, status, /* nsIUnicharInputStream */ unicharData)
  326.     {
  327.         if (LOG_LEVEL > 0 && this.m_bLogging) {
  328.             log("status: " + errorToString(status), this);
  329.         }
  330.         switch (status) {
  331.         case NS_BINDING_SUCCEEDED: {
  332.             var err = null;
  333.             var str = "";
  334.             try {
  335.                 if (unicharData) {
  336.                     var str_ = {};
  337.                     while (unicharData.readString(-1, str_)) {
  338.                         str += str_.value;
  339.                     }
  340.                 }
  341.                 if (LOG_LEVEL > 2 && this.m_bLogging) {
  342.                     log("contentCharset = " + this.onDetermineCharset(loader) +
  343.                         "\nrequest result:\n" + str, this);
  344.                 }
  345.             }
  346.             catch (exc) {
  347.                 err = exc;
  348.             }
  349.             this.execRespFunc(err, str);
  350.             break;
  351.         }
  352.         case NS_BINDING_REDIRECTED:
  353.         case NS_BINDING_RETARGETED:
  354.             // just status
  355.             // xxx todo: in case of a redirected channel,
  356.             // how to get that channel => cancel feature!
  357.             break;
  358.         default: // errors:
  359.             this.execRespFunc(status);
  360.             break;
  361.         }
  362.     }
  363. };
  364.  
  365. function issueNetworkRequest(parentRequest, respFunc, url, bLogging)
  366. {
  367.     var channel;
  368.     try {
  369.         var loader = Components.classes["@mozilla.org/network/unichar-stream-loader;1"]
  370.                                .createInstance(Components.interfaces.nsIUnicharStreamLoader);
  371.         channel = getIoService().newChannel(url, "" /* charset */, null /* baseURI */);
  372.         channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
  373.     }
  374.     catch (exc) {
  375.         respFunc(exc);
  376.         return;
  377.     }
  378.     var netRequest = new calWcapNetworkRequest(channel, respFunc, bLogging);
  379.     parentRequest.attachSubRequest(netRequest);
  380.     log("opening channel.", netRequest);
  381.     try {
  382.         loader.init(channel, netRequest, null /*context*/, 0 /*segment size*/);
  383.     }
  384.     catch (exc) {
  385.         netRequest.execRespFunc(exc);
  386.     }
  387. }
  388.  
  389. function getWcapRequestStatusString( xml )
  390. {
  391.     var str = "request status: ";
  392.     var items = xml.getElementsByTagName("RSTATUS");
  393.     if (items != null && items.length > 0)
  394.         str += items.item(0).textContent;
  395.     else
  396.         str += "none";
  397.     return str;
  398. }
  399.  
  400. function stringToIcal(data, expectedErrno)
  401. {
  402.     if (!data || data.length == 0) { // assuming time-out; WTF.
  403.         throw new Components.Exception(
  404.             "Login failed. Invalid session ID.",
  405.             Components.interfaces.calIWcapErrors.WCAP_LOGIN_FAILED);
  406.     }
  407.     var icalRootComp;
  408.     try {
  409.         icalRootComp = getIcsService().parseICS(data);
  410.     }
  411.     catch (exc) { // map into more useful error string:
  412.         throw new Components.Exception("error parsing ical data!",
  413.                                        Components.interfaces.calIErrors.ICS_PARSE);
  414.     }
  415.     checkWcapIcalErrno(icalRootComp, expectedErrno);
  416.     return icalRootComp;
  417. }
  418.  
  419. function stringToXml(data, expectedErrno)
  420. {
  421.     if (!data || data.length == 0) { // assuming time-out
  422.         throw new Components.Exception(
  423.             "Login failed. Invalid session ID.",
  424.             Components.interfaces.calIWcapErrors.WCAP_LOGIN_FAILED);
  425.     }
  426.     var xml = getDomParser().parseFromString(data, "text/xml");
  427.     checkWcapXmlErrno(xml, expectedErrno);
  428.     return xml;
  429. }
  430.  
  431.